package vn.hanelsoft.openwifi.model.delegate;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import vn.hanelsoft.openwifi.common.network_service.utils.HttpConnectionUtils;
import vn.hanelsoft.openwifi.model.object.ObjectWifi;
import android.os.Handler;

public class ServerDelegate {

	public static final String LINK_SERVER = "http://69.195.124.214/~paperco2";
	public static final String LINK_FACEBOOK_PROFILE = "https://graph.facebook.com/me";
	public static final String KEY_ACCESS_TOKEN = "access_token";
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_IMAGE = "images";
	public static Thread getFacebookProfile(final Handler handler, String access_token) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(KEY_ACCESS_TOKEN, access_token));
		final Runnable runnable = new Runnable() {
			@Override
			public void run() {
				HttpConnectionUtils.getFromURL(LINK_FACEBOOK_PROFILE, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	public static final String LINK_SERVER_GET_WIFI = LINK_SERVER+"/openwifi/getwifi.php";
	public static Thread getWifi(final Handler handler, String lat, String lon) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("lat", lat));
		params.add(new BasicNameValuePair("lon", lon));
		final Runnable runnable = new Runnable() {
			@Override
			public void run() {
				HttpConnectionUtils.getFromURL(LINK_SERVER_GET_WIFI, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	
	public static final String LINK_SERVER_SET_WIFI = LINK_SERVER+"/openwifi/insertwifi.php";
	public static final String KEY_BSSID = "BSSID";
	public static final String KEY_SSID = "SSID";
	public static final String KEY_PASS = "passkey";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_LAT = "lattitue";
	public static final String KEY_LON = "longitue";
	public static final String KEY_FACEBOOK_ID = "facebookid";
	public static final String KEY_FACEBOOK_NAME = "facebook_name";
	public static final String KEY_FACEBOOK_IMG = "facebook_img";
			
	public static Thread setWifi(final Handler handler, ObjectWifi mObjectWifi) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(KEY_BSSID, mObjectWifi.getWifiBSSID()));
		params.add(new BasicNameValuePair(KEY_SSID, mObjectWifi.getWifiSSID()));
		params.add(new BasicNameValuePair(KEY_PASS, mObjectWifi.getWifiPassword()));
		params.add(new BasicNameValuePair(KEY_EMAIL, mObjectWifi.getEmail()));
		params.add(new BasicNameValuePair(KEY_LAT, mObjectWifi.getLat()));
		params.add(new BasicNameValuePair(KEY_LON, mObjectWifi.getLng()));
		params.add(new BasicNameValuePair(KEY_FACEBOOK_ID, mObjectWifi.getFacebookId()));
		params.add(new BasicNameValuePair(KEY_FACEBOOK_NAME, mObjectWifi.getFacebookName()));
		params.add(new BasicNameValuePair(KEY_FACEBOOK_IMG, mObjectWifi.getFacebookImg()));
		final Runnable runnable = new Runnable() {
			@Override
			public void run() {
				HttpConnectionUtils.getFromURL(LINK_SERVER_SET_WIFI, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	public static final String LINK_SERVER_UPDATE_TOTAL_USER = LINK_SERVER+"/openwifi/updatetotaluser.php";
	public static Thread updateTotalUserWifi(final Handler handler, String BSSID, String noofuser) {
		final List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("BSSID", BSSID));
		params.add(new BasicNameValuePair("noofuser", noofuser));
		final Runnable runnable = new Runnable() {
			@Override
			public void run() {
				HttpConnectionUtils.getFromURL(LINK_SERVER_UPDATE_TOTAL_USER, params, handler, null);
			}
		};
		return HttpConnectionUtils.performOnBackgroundThread(runnable);
	}
	
	public static final String LINK_SERVER_UPDATE_TOTAL_TIME_SHOW_AD = LINK_SERVER+"/openwifi/updatetotaltimeshowads.php";
	public static Thread updateTotalTimeShowAd(final Handler handler, String BSSID, String adshowtotaltime) {
		if(!BSSID.equals("") && !adshowtotaltime.equals("")){
			final List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("BSSID", BSSID));
			params.add(new BasicNameValuePair("adshowtotaltime", adshowtotaltime));
			final Runnable runnable = new Runnable() {
				@Override
				public void run() {
					HttpConnectionUtils.getFromURL(LINK_SERVER_UPDATE_TOTAL_TIME_SHOW_AD, params, handler, null);
				}
			};
			return HttpConnectionUtils.performOnBackgroundThread(runnable);
		}
		return null;
	}
	
	public static final String LINK_SERVER_UPDATE_TOTAL_TIME_CLICK_AD = LINK_SERVER+"/openwifi/updatetotaltimeclickads.php";
	public static Thread updateTotalTimeClickAd(final Handler handler, String BSSID) {
		if(!BSSID.equals("")){
			final List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("BSSID", BSSID));
			final Runnable runnable = new Runnable() {
				@Override
				public void run() {
					HttpConnectionUtils.getFromURL(LINK_SERVER_UPDATE_TOTAL_TIME_CLICK_AD, params, handler, null);
				}
			};
			return HttpConnectionUtils.performOnBackgroundThread(runnable);
		}
		return null;
	}
}
